package com.mypage.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.mypage.bean.RelatedListBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuyongjie on 2017/5/10.
 */

public class NchangeListAdapter extends BaseAdapter {
    HolderView viewHolder = null;
    public List<RelatedListBean.RelatedData.mRelatedList> mList;
    public Context context;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer,Boolean> isSelected;
    private Map<Integer,Boolean> map=new HashMap<>();// 存放已被选中的CheckBox
    public NchangeListAdapter(List<RelatedListBean.RelatedData.mRelatedList> mList, Context context) {
        this.context = context;
        this.mList = mList;
        isSelected = new HashMap<Integer, Boolean>();
        // 初始化数据
        initDate();
    }
    public void initDate(){
        for(int i=0; i<mList.size();i++) {
            getIsSelected().put(i,false);
        }
    }
    @Override
    public int getCount() {

        return mList.size() == 0 ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            viewHolder = new HolderView();
            convertView = View.inflate(context, R.layout.change_list_item, null);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
            viewHolder.textContent = (TextView) convertView.findViewById(R.id.textContent);
            viewHolder.textNumber = (TextView) convertView.findViewById(R.id.textNumber);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (HolderView) convertView.getTag();
        }
        viewHolder.textContent.setText(mList.get(position).name.toString());
        viewHolder.textNumber.setText("("+mList.get(position).relationSize+")");
        viewHolder.checkBox.setChecked(getIsSelected().get(position));

        return convertView;
    }

    public class HolderView {
        public CheckBox checkBox;
        public TextView textNumber;
        public TextView textContent;
    }

    public static HashMap<Integer,Boolean> getIsSelected() {
        return isSelected;
    }
    public static void setIsSelected(HashMap<Integer,Boolean> isSelected) {
        NchangeListAdapter.isSelected = isSelected;
    }
}
